package com.bravo.ar.activities;

import java.io.FileOutputStream;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.bravo.ar.R;
import com.metaio.sdk.ARViewActivity;
import com.metaio.sdk.MetaioDebug;
import com.metaio.sdk.jni.IGeometry;
import com.metaio.sdk.jni.IMetaioSDKCallback;
import com.metaio.sdk.jni.Vector3d;
import com.metaio.tools.io.AssetsManager;

public class Ardemo extends ARViewActivity {
	private IGeometry mImagePlane1;
	private IGeometry mImagePlane2;
	private IGeometry mImagePlane3;

	@Override
	protected int getGUILayout() {
		return R.layout.ardemo;
	}

	public void onButtonClick(View v) {
		finish();
	}

	@Override
	protected void loadContents() {
		try {
			
			AssetsManager.extractAllAssets(this, true); 
			// Getting a file path for tracking configuration XML file
			//String contextdir = Context.
			String trackingConfigFile = AssetsManager
					.getAssetPath("ArDemo/Assets/TrackingData_MarkerlessFast.xml");
             
			// Assigning tracking configuration
			boolean result = metaioSDK
					.setTrackingConfiguration(trackingConfigFile);
			MetaioDebug.log("Tracking data loaded: " + result);

			final String imagePath1 = AssetsManager
					.getAssetPath("ArDemo/Assets/billboard_green.png");
			if (imagePath1 != null) {
				mImagePlane1 = metaioSDK.createGeometryFromImage(imagePath1);
				if (mImagePlane1 != null) {
					mImagePlane1.setScale(1.5f);
					mImagePlane1.setCoordinateSystemID(1);
					mImagePlane1.setVisible(true);
					mImagePlane1.setTranslation(new Vector3d(150, 150, 0));
					MetaioDebug.log("Loaded geometry " + imagePath1);
				} else {
					MetaioDebug.log(Log.ERROR, "Error loading geometry: "
							+ imagePath1);
				}
			}
			final String imagePath2 = AssetsManager
					.getAssetPath("ArDemo/Assets/billboard_blue.png");
			if(imagePath2!=null){	
				mImagePlane2 = metaioSDK.createGeometryFromImage(imagePath2);
				if (mImagePlane2 != null) {
					mImagePlane2.setScale(1f);
					mImagePlane2.setCoordinateSystemID(2);
					mImagePlane2.setVisible(true);
					mImagePlane2.setTranslation(new Vector3d(150, 50, 0));
					MetaioDebug.log("Loaded geometry " + imagePath2);
				} else {
					MetaioDebug.log(Log.ERROR, "Error loading geometry: "
							+ imagePath2);
				}
				
			}
			final String imagePath3 = AssetsManager
					.getAssetPath("ArDemo/Assets/billboard_red.png");
			if(imagePath3!=null){
				mImagePlane3 = metaioSDK.createGeometryFromImage(imagePath3);
				if (mImagePlane3 != null) {
					mImagePlane3.setScale(1f);
					mImagePlane3.setCoordinateSystemID(3);
					mImagePlane3.setVisible(true);
					mImagePlane3.setTranslation(new Vector3d(200, 200, 0));
					MetaioDebug.log("Loaded geometry " + imagePath3);
				} else {
					MetaioDebug.log(Log.ERROR, "Error loading geometry: "
							+ imagePath3);
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onGeometryTouched(IGeometry geometry) {
		Toast.makeText(Ardemo.this, "亲，模型被单击，我要调转到哪呢？？", Toast.LENGTH_LONG).show();
	}

	@Override
	protected IMetaioSDKCallback getMetaioSDKCallbackHandler() {
		return null;
	}

	/**
	 * crate imegTitle
	 * 
	 * @param billBoardTitle
	 * @return
	 */
	private String createBillboardTexture(String billboardPath,String billBoardTitle) {
		try {
			final String texturepath = getCacheDir() + "/" + billBoardTitle
					+ ".png";
			Paint mPaint = new Paint();

			// Load background image (256x128), and make a mutable copy
			Bitmap billboard = null;

			// reading billboard background
			String filepath = AssetsManager
					.getAssetPath("ArDemo/Assets/"+billboardPath);
			Bitmap mBackgroundImage = BitmapFactory.decodeFile(filepath);

			billboard = mBackgroundImage.copy(Bitmap.Config.ARGB_8888, true);

			Canvas c = new Canvas(billboard);

			mPaint.setColor(Color.WHITE);
			mPaint.setTextSize(24);
			mPaint.setTypeface(Typeface.DEFAULT);

			float y = 40;
			float x = 30;

			// Draw POI name
			if (billBoardTitle.length() > 0) {
				String n = billBoardTitle.trim();
				final int maxWidth = 160;

				int i = mPaint.breakText(n, true, maxWidth, null);
				c.drawText(n.substring(0, i), x, y, mPaint);

				// Draw second line if valid
				if (i < n.length()) {
					n = n.substring(i);
					y += 20;
					i = mPaint.breakText(n, true, maxWidth, null);
					if (i < n.length()) {
						i = mPaint.breakText(n, true, maxWidth - 20, null);
						c.drawText(n.substring(0, i) + "...", x, y, mPaint);
					} else {
						c.drawText(n.substring(0, i), x, y, mPaint);
					}
				}

			}

			// writing file
			try {
				FileOutputStream out = new FileOutputStream(texturepath);
				billboard.compress(Bitmap.CompressFormat.PNG, 90, out);
				MetaioDebug.log("Texture file is saved to " + texturepath);
				return texturepath;
			} catch (Exception e) {
				MetaioDebug.log("Failed to save texture file");
				e.printStackTrace();
			}

			billboard.recycle();
			billboard = null;

		} catch (Exception e) {
			MetaioDebug.log("Error creating billboard texture: "
					+ e.getMessage());
			MetaioDebug.printStackTrace(Log.DEBUG, e);
			return null;
		}
		return null;
	}
}
